package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.entity.UserToken;
import com.qfedu.openplateform.manager.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    /**
     * token管理----->查询所有token
     * @param userToken
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping( "/table")
    public TableData table(UserToken userToken, Integer page, Integer limit) {
        PageInfo<UserToken> pageInfo = tokenService.getTokenList(userToken, page, limit);
        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * token管理----->删除token
     */

    @RequestMapping( "/del")
    public AjaxMessage delete(int[] ids) {
        try {
            tokenService.deleteToken(ids);
            return new AjaxMessage(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "删除失败");
    }


    /**
     * token管理----->修改token
     * @param userToken
     * @return
     */
    @RequestMapping( "/update")
    public AjaxMessage update(UserToken userToken) {
        try {
            tokenService.updateToken(userToken);
            return new AjaxMessage(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "修改失败");
    }

}
